﻿#include "VAOneXMLAdaptorFrequencyDomain.h"
#include "rapidxml.hpp"
#include "FITK_Component/FITKAcousticsPhysics/FITKGlobalFrequencyDomain.h"
#include <QFile>
#include <iostream>

namespace IO
{
    bool VAOneXMLAdaptorFrequencyDomain::adaptR()
    {
        // 返回操作是否成功
        return readFrequencyDomain();
    }

    bool VAOneXMLAdaptorFrequencyDomain::adaptW()
    {
        // 返回操作是否成功
        return writeFrequencyDomain();
    }

    bool VAOneXMLAdaptorFrequencyDomain::readFrequencyDomain()
    {
        if(!_dataObj)
        {
            return false;
        }

        if(!_node)
        {
            return false;
        }

        //将数据对象转换为频率对象
        auto frequencyDomain = dynamic_cast<Acoustics::FITKGlobalFrequencyDomain*>(_dataObj);

        if(!frequencyDomain)
        {
            return false;
        }

        // 获取XML中的FrequencyDomain节点
        rapidxml::xml_node<char>* realSpectrumNode = _node->first_node("FrequencyDomain");

        // 检查节点是否存在
        if (!realSpectrumNode)
        {
            //std::cout << "No 'FrequencyDomain' node found. Exiting.";
            return false;
        }

        bool b = false;
        // 读取domain_type属性
        int domainType = readDoubleAttribute(realSpectrumNode, "domain_type", &b);
        Acoustics::FITKGlobalFrequencyDomain::DomainType t = static_cast<Acoustics::FITKGlobalFrequencyDomain::DomainType>(domainType);
        // 设置频域类型
        if (b)
        {
            frequencyDomain->setDomainType(t);
        }

        // 读取起始频率属性
        double startFreq = readDoubleAttribute(realSpectrumNode, "start_freq",   &b);

        if (b)
        {
            frequencyDomain->setStartFrequence(startFreq);
        }

        // 读取结束频率属性
        double endFreq = readDoubleAttribute(realSpectrumNode, "end_freq", &b);

        if (b)
        {
            frequencyDomain->setEndFrequence(endFreq);
        }

        // 读取带宽
        double bandFreq = readDoubleAttribute(realSpectrumNode, "bandwidth_freq", &b);

        if (b)
        {
            frequencyDomain->setBandWidth(bandFreq);
        }
        // 成功读取，返回true
        return true;
    }

    bool VAOneXMLAdaptorFrequencyDomain::writeFrequencyDomain()
    {
        // 将数据对象转换为频域对象
        Acoustics::FITKGlobalFrequencyDomain* frequencyDomain = dynamic_cast<Acoustics::FITKGlobalFrequencyDomain*>(_dataObj);

        if (!_node || !frequencyDomain)
        {
            return false;
        }

        // 为FrequencyDomain创建XML节点
        rapidxml::xml_node<char>* frequencyNode = createNodeElement(_node, "FrequencyDomain");

        if(!frequencyNode)
        {
            return false;
        }

        // 将属性写入FrequencyDomain节点
        writeAttribute(frequencyNode, "start_freq", frequencyDomain->getStartFrequence());
        writeAttribute(frequencyNode, "end_freq", frequencyDomain->getEndFrequence());
        writeAttribute(frequencyNode, "domain_type", static_cast<int>(frequencyDomain->getDomainType()));
        writeAttribute(frequencyNode, "bandwidth_freq", frequencyDomain->getBandWidth());
        //写入成功
        return true;
    }

}
